package controller;

import javax.swing.JOptionPane;

import util.MoveType;
import entity.Board;
import entity.Move;
import entity.Word;
import gui.BoardApplication;

/**
 * This is the controller for protecting a word.
 */
public class ProtectController {
	private Board board;
	private BoardApplication gui;
	
	/**
	 * Constructor of ProtectController.
	 */
	public ProtectController(BoardApplication gui, Board board){
		this.board = board;
		this.gui = gui;
	}
	
	/**
	 * Protect a word.
	 */
	public boolean protectWord(Word w){
		double protectedWordsCount = board.getProtectedWords().size();
		double unprotectedWordsCount = board.getUnprotectedWords().size();
		double totalWords = protectedWordsCount + unprotectedWordsCount;
		
		if((protectedWordsCount + 1) / totalWords < 0.8){
			board.protectWord(w);
			gui.updateTable(board.getUnprotectedWords());
			return true;
		}else{
			JOptionPane.showMessageDialog(null, "You can't protect more than 80% of the words in your Board. \nTry unprotecting some unused words first.", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		return false;
	}
	
	/**
	 * Return board.
	 */
	public Board getBoard() {
		return board;
	}

	/**
	 * Return gui.
	 */
	public BoardApplication getGui() {
		return gui;
	}
}
